package com.bby.user.controller;

import com.bby.common.aop.ParseToken;
import com.bby.common.exception.BBYException;
import com.bby.common.model.PmMessage;
import com.bby.common.model.PmMessageDeal;
import com.bby.common.utils.BBYUtils;
import com.bby.common.utils.R;
import com.bby.user.service.MessageService;
import org.springframework.web.bind.annotation.*;

/**
 * 用户-权限请求
 */
@RestController
@RequestMapping("message")
public class MessageController {

    MessageService messageService;

    public MessageController(MessageService messageService) {
        this.messageService = messageService;
    }

    /**
     * 创建消息，必须登录
     * @param message 消息
     */
    @ParseToken()
    @PostMapping("add")
    public R add(String userId,@RequestBody PmMessage message) throws BBYException {
        messageService.addPmMessage(userId,message);
        return R.ok();
    }
    /**
     * 删除消息，必须登录
     * @param messageId 消息id
     */
    @ParseToken(require = true)
    @RequestMapping("del")
    public R delete(String userId, String messageId) throws BBYException {
        messageService.deletePmMessage(userId,messageId);
        return R.ok();
    }

    /**
     * 获取所有信息,必须登录
     * @param action send/receive
     * @param all 拉取全部还是未处理的
     */
    @ParseToken(require = true)
    @GetMapping("list")
    public R list(String userId,
                  @RequestParam(defaultValue = "receive") String action,
                  @RequestParam(defaultValue = "false") Boolean all) throws BBYException {
        BBYUtils.checkEmpty(action,all);
        return R.ok(messageService.listPmMessage(userId,action,all));
    }

    /**
     * 处理消息,必须登录
     * @param deal 参数
     */
    @ParseToken(require = true)
    @PostMapping("deal")
    public R deal(String userId,@RequestBody PmMessageDeal deal) throws BBYException {
        messageService.dealPmMessage(userId,deal);
        return R.ok();
    }
}
